package com.lang.daoandimpl;

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lang.beans.Parcel;
import com.lang.util.Config;

/**
 * A data access object (DAO) providing persistence and search support for
 * Parcel entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.lang.daoandimpl.Parcel
 * @author MyEclipse Persistence Tools
 */

public class ParcelDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(ParcelDAO.class);
	// property constants

	public static final String USER_ID = "user.id";
	public static final String SEND_NAME = "sendName";
	public static final String SEND_TEL = "sendTel";
	public static final String SEND_ADDR = "sendAddr";
	public static final String REC_NAME = "recName";
	public static final String REC_TEL = "recTel";
	public static final String REC_ADDR = "recAddr";
	public static final String SIZE = "size";
	public static final String TYPE = "type";
	public static final String WEIGHT = "weight";
	public static final String TRANS_TYPE = "transType";
	public static final String STATUS = "status";
	public static final String PAY_TYPE = "payType";
	public static final String COMMENT = "comment";

	protected void initDao() {
		// do nothing
	}

	public void save(Parcel transientInstance) {
		log.debug("saving Parcel instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Parcel persistentInstance) {
		log.debug("deleting Parcel instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Parcel findById(java.lang.Integer id) {
		log.debug("getting Parcel instance with id: " + id);
		try {
			Parcel instance = (Parcel) getHibernateTemplate().get(
					"com.lang.beans.Parcel", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Parcel instance) {
		log.debug("finding Parcel instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Parcel instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Parcel as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 通过用户id 查询用户的包裹
	 * 
	 * @param userId
	 * @return
	 */
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findBySendName(Object sendName) {
		return findByProperty(SEND_NAME, sendName);
	}

	public List findBySendTel(Object sendTel) {
		return findByProperty(SEND_TEL, sendTel);
	}

	public List findBySendAddr(Object sendAddr) {
		return findByProperty(SEND_ADDR, sendAddr);
	}

	public List findByRecName(Object recName) {
		return findByProperty(REC_NAME, recName);
	}

	public List findByRecTel(Object recTel) {
		return findByProperty(REC_TEL, recTel);
	}

	public List findByRecAddr(Object recAddr) {
		return findByProperty(REC_ADDR, recAddr);
	}

	public List findBySize(Object size) {
		return findByProperty(SIZE, size);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByWeight(Object weight) {
		return findByProperty(WEIGHT, weight);
	}

	public List findByTransType(Object transType) {
		return findByProperty(TRANS_TYPE, transType);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByPayType(Object payType) {
		return findByProperty(PAY_TYPE, payType);
	}

	public List findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

	public List findAll() {
		log.debug("finding all Parcel instances");
		try {
			String queryString = "from Parcel";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	/**
	 * 更新异常记录
	 * @param detachedInstance
	 * @return
	 * @author Lang
	 */
	public void updateAbnormal(int id,String comment){
		try {
			Parcel p=getHibernateTemplate().get(Parcel.class,id);
			p.setComment(comment);   //更改备注
			p.setStatus(Config.PARCEL_DEAL_ABNORMAL);
			getHibernateTemplate().update(p);
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}	
	}
	/**
	 * 更新成功派件
	 * @param id
	 * @author Lang
	 */
	public void updateSuccess(int id){
		try {
			Parcel p=getHibernateTemplate().get(Parcel.class,id);
			p.setStatus(Config.PARCEL_DEAL_SUCCESS);
			getHibernateTemplate().update(p);
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}	
	}
	
	/**
	 * 更新正在派件
	 * @param id
	 * @author Lang
	 */
	public void updateDealing(int id){
		try {
			Parcel p=getHibernateTemplate().get(Parcel.class,id);
			p.setStatus(Config.PARCEL_DEALING);
			getHibernateTemplate().update(p);
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}	
	}

	
	public Parcel merge(Parcel detachedInstance) {
		log.debug("merging Parcel instance");
		try {
			Parcel result = (Parcel) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Parcel instance) {
		log.debug("attaching dirty Parcel instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Parcel instance) {
		log.debug("attaching clean Parcel instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ParcelDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ParcelDAO) ctx.getBean("ParcelDAO");
	}
}